package br.com.proway.cursovipjava.telas;

import java.awt.Color;
import java.awt.Component;
import java.awt.Cursor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.TableColumn;

import br.com.proway.biblioteca.Editora;

public class ListarEditoras extends JFrame{


	/**
	 * 
	 * @throws Exception
	 */
	public ListarEditoras() throws Exception {
		super();
		setTitle("Programa Treinamento java proway");
		setLocation(100, 100);
		setSize(400, 400);
		setLayout(null);		
		setContentPane(getConteudo());
		setJMenuBar(retornaMenu());
		setDefaultCloseOperation(EXIT_ON_CLOSE);
	}
	
	/**
	 * 
	 * @return
	 * @throws Exception
	 */
	public JPanel getConteudo() throws Exception {
		JPanel jp = new JPanel();
		jp.setLayout(null);
		jp.setSize(200, 400);
		jp.setBackground(new Color(250, 200, 200));
	    jp.add(getListaDeResultados());
		return jp;
	}
	
	/**
	 * 
	 * @return
	 * @throws Exception
	 */
	private JScrollPane getListaDeResultados() throws Exception {
	    JScrollPane pane = new JScrollPane( getListas());
	    pane.setSize(400, 400);
	  
	    return pane;
	}


	public JMenuItem retornarItemMenuCadastrarEditora() {
		JMenuItem jmenuItem = new JMenuItem("Cadastrar Editora");
		jmenuItem.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				try {
					CadastrarEditora ce = new CadastrarEditora();
					ce.setVisible(true);
					hide(); 

				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
		});
		
		return jmenuItem;
	}
	
	public JMenuItem retornarItemMenuCadastrarEstado() {
		JMenuItem jmenuItem = new JMenuItem("Cadastrar Estado");
		jmenuItem.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				try {
					CadastrarEstado ce = new CadastrarEstado();
					ce.setVisible(true);
					hide(); 

				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
		});
		
		return jmenuItem;
	}

	public JMenuItem retornarItemMenuArquivoSair() {
		JMenuItem jmenuItem = new JMenuItem("Sair");
		jmenuItem.setMnemonic('S');
		jmenuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				System.exit(0);
			}
		});
		return jmenuItem;
	}

	public JMenu retornarMenuArquivo() {
		JMenu jmenuArquivo = new JMenu("Arquivo");
		jmenuArquivo.setMnemonic('A');
		jmenuArquivo.add(retornarItemMenuCadastrarEstado());
		jmenuArquivo.add(retornarItemMenuCadastrarEditora());
		jmenuArquivo.add(retornarItemMenuArquivoSair());
		return jmenuArquivo;
	}

	public JMenu retornarMenuEditar() {
		JMenu jmenuArquivo = new JMenu("Listas");
		jmenuArquivo.setMnemonic('E');
		jmenuArquivo.add(retornarItemMenuListarEditora());
		
		return jmenuArquivo;
	}

	private JMenuItem retornarItemMenuListarEditora() {
		JMenuItem jmenuItem = new JMenuItem("Listar Editora");
		jmenuItem.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				try {
					CadastrarEstado ce = new CadastrarEstado();
					ce.setVisible(true);
					hide(); 

				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
		});
		
		return jmenuItem; 
	}

	public JMenuBar retornaMenu() {
		JMenuBar jmenubar = new JMenuBar();
		jmenubar.setCursor(new Cursor(Cursor.HAND_CURSOR));
		jmenubar.add(retornarMenuArquivo());
		jmenubar.add(retornarMenuEditar());
		return jmenubar;
	}
	/**
	 * 
	 * @return
	 * @throws Exception
	 */
	public JTable getListas() throws Exception{
		String[] cabecalho = new String[]{"Nome", "Site", "Cidade"};
		Editora e = new Editora();
		ResultSet rs = e.ConsultarEditora(null, null);
		ArrayList<String[]> lista = converterResultSetToArrayList(rs);
		System.out.println(lista.size());
		String[][] retorno = new String[lista.size()][];
		for (int cont = 0; cont < lista.size();cont++){
			retorno[cont] = (String[])lista.get(cont);
		}
		JTable jt = new JTable(retorno,  cabecalho);
		jt.setSize(400, 400);
				
		jt.addMouseListener(new MouseListener() {
			
			public void mouseReleased(MouseEvent arg0) {
				// TODO Auto-generated method stub
				//System.out.println("mouse released");
				
			}
			
			public void mousePressed(MouseEvent arg0) {
				JTable jt1 = (JTable)arg0.getSource();
				System.out.println(jt1.getValueAt(jt1.getSelectedRow(), 0));
			}
			
			public void mouseExited(MouseEvent arg0) {
			}
			
			public void mouseEntered(MouseEvent arg0) {
				// TODO Auto-generated method stub
				//System.out.println("mouse entered");
			}

			public void mouseClicked(MouseEvent arg0) {
				// TODO Auto-generated method stub
				System.out.println("mouse clicked");
			}
		});
		return jt; 
	}

	/**
	 * 
	 * @param rs
	 * @return
	 * @throws SQLException
	 */
	private ArrayList<String[]> converterResultSetToArrayList(ResultSet rs) throws SQLException {
		ArrayList<String[]> lista =new ArrayList<String[]>();
		while (rs.next()){
			String[] linha = new String[]{rs.getString("nome"), rs.getString("siteeditora"), rs.getString("cidade")};
			lista.add(linha);
		}
		return lista;
	}
	/**
	 * 
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		ListarEditoras lc = new ListarEditoras();
		lc.setVisible(true);
	}
}
